package com.javaGather.demo1;

import com.sun.nio.sctp.PeerAddressChangeNotification;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Test4 {
    public static void main(String[] args) {
        /**
         * hashMap集合遍历的方式
         */

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("1", "a");
        hashMap.put("2", "b");
        hashMap.put("3", "c");
        hashMap.put("4", "d");
        hashMap.put("5", "e");

        /**
         * 方式一：1.获取所有的key集合  2.通过get方法得到对应value
         */
        Set<String> keySet = hashMap.keySet();
        for (String key : keySet) {
            System.out.println(key + "-" + hashMap.get(key));
        }
        System.out.println("===============");

        /**
         * 方式二：entrySet集合
         */
        //Set<Map.Entry<String, String>> entries = hashMap.entrySet();
        for (Map.Entry<String, String> entry : hashMap.entrySet()) {
            System.out.println(entry.getKey() + "-" + entry.getValue());
        }
        System.out.println("===============");

        /**
         * 方式三:hashMap.entrySet()+Iterator
         */
        Set<Map.Entry<String, String>> entries = hashMap.entrySet();
        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        if (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            System.out.println(entry.getKey() + "-" + entry.getValue());
        }


    }
}
